package prop;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;

import org.apache.log4j.Logger;

//import com.asb.minimediation.util.INMSLog;

public class EventFilter {
	
	private static Properties eventNameProps = new Properties();
	private static Properties alarmNameProps = new Properties();
	private static Properties alarmNameLikeProps = new Properties();
    private static EventFilter filter = null;

    public static List<String> eventNameList = null;
    public static List<String> alarmNameList = null;
    public static List<String> alarmNameLikeList = null;

    //Logger adapterlog = INMSLog.getInstance().getAdapterLog();
    
    private EventFilter() {
        eventNameList = new ArrayList<String>();
        alarmNameList = new ArrayList<String>();
        alarmNameLikeList = new ArrayList<String>();
        try {
        	//eventNameProps.load(new FileInputStream("../conf/filter_events.properties"));
        	alarmNameProps.load(new FileInputStream("./conf/filter_alarms.properties"));
        	//alarmNameLikeProps.load(new FileInputStream("../conf/filter_like_alarms.properties"));
		} catch (FileNotFoundException e) {
			//adapterlog.info(e.getMessage());
			e.printStackTrace();
		} catch (IOException e) {
			//adapterlog.info(e.getMessage());
			e.printStackTrace();
		}
		Enumeration eventEnu = eventNameProps.keys();
		Enumeration alarmEnu = alarmNameProps.keys();
		Enumeration alarmLikeEnu = alarmNameLikeProps.keys();
		while (eventEnu.hasMoreElements()) {
			eventNameList.add(eventEnu.nextElement().toString().trim());
		}
		while (alarmEnu.hasMoreElements()) {
			alarmNameList.add(alarmEnu.nextElement().toString().trim());
		}
		while (alarmLikeEnu.hasMoreElements()) {
			alarmNameLikeList.add(alarmLikeEnu.nextElement().toString().trim());
		}
    }

    public static synchronized EventFilter getInstance() {
    	if(null==filter){
    		filter = new EventFilter();
    	}
        return filter;
    }

    /**
     * 根据告警名称来判断该事件是否屏蔽
     * @param name
     * @return
     */
    public boolean isContainEvent(String name) {
    	return eventNameList.contains(name);
    }
    
    /**
     * 根据告警名称来判断该告警是否屏蔽
     * @param name
     * @return
     */
    public boolean isContainAlarm(String name) {
    	return alarmNameList.contains(name) || isLikeAlarm(name);
    }
    
    //模糊匹配
    private boolean isLikeAlarm(String name) {
    	for (String subAlarm : alarmNameLikeList) {
    		if (name.contains(subAlarm)) {
    			return true;
    		}
    	}
    	return false;
    }
    
    public static void main(String[] args) {
    	EventFilter filter = getInstance();
    	//System.out.println(filter.isContainAlarm(""));
    	//System.out.println(filter.isContainEvent(""));
    	System.out.println(alarmNameList.size());
    	for (int i = 0; i < alarmNameList.size(); i++){
    		System.out.println(alarmNameList.get(i));
    	}
	}
}
